<template>
  <div class="permission">
    <div class="operate">
      <div>
        <ep-search
            v-model="queryParam.h2102"
            :size="size"
            placeholder="请输入编号"
            enter-button="chaxun"
            style="width:15vw;"
            @search="
            () => {
              this.listQuery();
            }
          "
        ></ep-search>
        <ep-button
            style="margin-left: 15px"
            :size="size"
            icon="refresh.svg"
            @click="
            () => {
              this.flushPage();
            }
          "
        ></ep-button>
        <a-tree-select
            placeholder="请选择区划范围(可多选)"
            ref="taskArea"
            allowClear
            tree-checkable
            style="margin-left: 15px;width:16vw"
            v-model="queryParam.manageAreas"
            :show-checked-strategy="SHOW_PARENT"
            :treeData="taskAreaTree"
            @change="manageAreaChange()"
            :size="size"
        />
        <ep-button
            style="margin-left: 15px"
            :size="size"
            icon="add.svg"
            @click="
            () => {
              this.addProject();
            }
          "
        >新增
        </ep-button>
        <ep-button
            :size="size"
            style="margin-left: 15px"
            type="danger"
            icon="del.svg"
            @click="
            () => {
              this.deleteProject();
            }
          "
        >
          删除
        </ep-button>
<!--        <ep-button-->
<!--            :size="size"-->
<!--            style="margin-left: 15px"-->
<!--            @click="-->
<!--            () => {-->
<!--              this.importData();-->
<!--            }-->
<!--          "-->
<!--        >导入</ep-button-->
<!--        >-->
      </div>

      <div>
        <column-manage :column.sync="tableColumn" @change="columnChange"></column-manage>
        <ep-button
            :size="size"
            style="margin-left: 15px"
            icon="export.svg"
            @click="
            () => {
              this.exportExcel();
            }
          "
        >导出</ep-button
        >
      </div>
    </div>

    <a-modal
        centered
        style="top: 20px"
        width="75%"
        destroyOnClose
        v-model="visible"
        cancelText="关闭"
        okText="保存"
        @ok="handleOk"
        @cancel="handleCancel"
    >
      <ancient-wharf-add-form
          ref="ancientWharfAddForm"
          @handleCancel="handleCancel"
          @showOrHide="showOrHide"
          :listQuery="listQuery">
      </ancient-wharf-add-form>
      <template slot="footer">
        <ep-button @click="handleCancel">取消</ep-button>
        <ep-button @click="handleOk" type="save">保存</ep-button>
      </template>
    </a-modal>

    <div class="ep-table">
      <zh-table
          ref="tableList"
          style="width: 100%"
          :data="tableData"
          height="auto"
          rowId="roleId"
          type="paging"
          :url = "tableUrl"
          :columns="tableColumn"
          @checkbox-change="selectChangeEvent"
          @checkbox-all="selectAllEvent"
      >
        <template #filter="{ $panel, column }">
          <input
              type="type"
              v-for="(option, index) in column.filters"
              :key="index"
              v-model="option.data"
              @input="$panel.changeOption($event, true, option)"
          />
        </template>
        <template #operate="{ row }">
          <div class="alert-message">
            <i class="fa fa-exclamation-circle alert-message-icon"></i>
            <span class="alert-message-content">
<!--              <ep-button @click="showDetail(row)"> 查看</ep-button>-->
              <ep-button style="margin-left: 5px" @click="editDetail(row)"> 编辑</ep-button>
            </span>
          </div>
        </template>
        <template #href="{ row }">
          <a target="_blank" :href="'https://'+row.h2113" >{{ row.h2113 }}</a>
        </template>
      </zh-table>
    </div>
  </div>
</template>

<script>
import modal from "ant-design-vue/es/modal";
const SHOW_PARENT = TreeSelect.SHOW_PARENT;
import {
  getAncientWharfList,
  ancientWharfDelete,
  getPermissionParams,
} from "@/services/system/whdngxlApi";
import ZhTable from "@/components/zh-table/ZhTable";
import { getBatchDict } from "@/services/system/api";
import UnmovableRelics from "@/pages/whdngxl/TasksCheckManage/UnmovableRelics";
import ColumnManage from "@/components/assembly/columnManage";
import {TreeSelect} from "ant-design-vue";
import {mapGetters} from "vuex";
import ancientWharfAddForm from "@/pages/system/form/AncientWharfAddForm";
export default {
  name: "AncientWharfManage",
  computed: {
    ...mapGetters(['size']),
  },
  components: {
    ancientWharfAddForm,
    ColumnManage,
    ZhTable,
  },
  data() {
    return {
      dictData: {
        h2106:[]
      },
      filtersData: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      // 分页参数
      tablePage: {
        total: 0,
        currentPage: 1,
        pageNum: 1,
        pageSize: 200,
      },
      queryParam: {
        h2102: "",
        manageAreas: undefined,
      },
      taskAreaTree: [],
      SHOW_PARENT,
      tableColumn: [
        { type: "checkbox", width: 50, fixed: "left", visible: true },
        // {type: 'seq', width: 80, fixed: 'left', align: 'center', title: '序号',visible: true},
        {
          field: "h2101",
          title: "古码头数字档案ID",
          visible: false,
          align: "center",
          width: 150,
        },
        {
          field: "h2115",
          title: "区划",
          align: "center",
          width: 120,
          visible: true,
        },
        {
          field: "h2102",
          title: "编号",
          align: "center",
          width: 200,
          visible: true,
        },
        {
          field: "h2103",
          title: "详情位置",
          align: "center",
          width: 200,
          visible: true,
        },
        {
          field: "h2104",
          title: "发掘尺寸",
          align: "center",
          width: 150,
          visible: true,
        },
        {
          field: "h2105",
          title: "结构特点",
          align: "center",
          width: 120,
          visible: true,
        },
        {
          field: "h2106Name",
          title: "保护级别",
          align: "center",
          width: 150,
          visible: true,
          filters: [],
        },
        {
          field: "h2107",
          title: "文物价值",
          align: "center",
          width: 120,
          visible: true,
        },
        {
          field: "h2108",
          title: "保护状况",
          align: "center",
          width: 120,
          visible: true,
        },
        {
          field: "h2110",
          title: "巡查文保员",
          align: "center",
          width: 150,
          visible: true,
        },
        {
          field: "h2112",
          title: "点位负责人",
          align: "center",
          width: 150,
          visible: true,
        },
        {
          field: "h2113",
          title: "三维超链接",
          align: "center",
          width: 250,
          visible: true,
          slots: {
            // 使用插槽模板渲染
            default: "href",
          },
        },
        {
          field: "operate",
          title: "操作",
          fixed: "right",
          align: "center",
          width: 100,
          slots: {
            // 使用插槽模板渲染
            default: "operate",
          },
          visible: true,
        },
      ],
      tableData: [],
      visible: false,
      //表头
      toolbarConfig: {
        custom: true,
        slots: {},
      },
      loading: false,
      selectedRowKeys: [] /*table选中keys*/,
      selectionRows: [] /*table选中records*/,
      tableUrl: getAncientWharfList,
    };
  },
  mounted() {},
  created() {
    //获取任务范围
    this.$http.post(getPermissionParams, {}).then((res) => {
      const data = res.result;
      this.taskAreaTree = data.orgTreeData;
    });
    // 数据初始化
    this.$nextTick(() => {
      this.init();
    });
  },
  methods: {
    manageAreaChange() {
      this.listQuery();
    },
    init() {
      const params2 = {};
      params2.dictFields = "b0105";
      this.$http.post(getBatchDict, params2).then((res) => {
        this.dictData.h2106 = res.result.b0105;
        //获取筛选数据
        UnmovableRelics.methods.getFiltersData(this, "tableList");
      });
      this.listQuery();
    },
    handleOk() {
      this.$nextTick(() => {
        this.$refs.ancientWharfAddForm.confirm();
      });
    },
    handleCancel() {
      this.visible = false;
    },
    showOrHide() {
      this.visible = !this.visible;
    },
    listQuery() {
      //手动清空用户的选择
      this.$refs.tableList.clearCheckboxRow()
      //手动清空用户保留选中的行数据
      this.$refs.tableList.clearCheckboxReserve()
      //删除所选中的数据
      this.$refs.tableList.removeCheckboxRow()
      this.selectionRows = []
      this.selectedRowKeys = []
      this.$refs.tableList.isAllChecked = false
      this.$refs.tableList.query(this.queryParam)
    },
    addProject() {
      this.showOrHide();
    },
    deleteProject() {
      if (this.$refs.tableList.isAllChecked) {
        // this.selectionRows=this.$refs.tableList.allData;
        this.selectionRows = this.$refs.tableList.getCheckboxRecords();
      } else {
        this.selectionRows = this.$refs.tableList.getAllCheckboxRecords();
      }
      if (this.selectionRows.length === 0) {
        modal.warn({ title: "提示", content: "请至少选择一条数据" });
        return;
      }
      modal.confirm({
        title: "确认删除吗？",
        onOk: () => {
          const params = {};
          this.$loading.show("正在处理数据,请等待...");
          params.h21VOList = this.selectionRows;
          setTimeout(() => {
            this.$http
                .post(ancientWharfDelete, params)
                .then((res) => {
                  this.$message.info("删除成功");
                  this.init();
                  this.$loading.hide();
                })
                .catch((error) => {
                  console.log(JSON.stringify(error));
                  this.$loading.hide();
                })
                .finally(() => {});
          }, 50);
        },
        onCancel: () => {},
      });
    },
    exportExcel() {
      this.$refs.tableList.exportData("文物标本数字档案管理",'csv')

    },
    importData(){

    },
    flushPage() {
      this.queryParam.h2102 = "";
      this.init();
    },
    //查看详情
    /*  showDetail(row) {
        this.showOrHide();
        this.$nextTick(() => {
          this.$refs.ancientWharfAddForm.initData(row,'view');
        });
      },*/
    editDetail(row){
      this.showOrHide();
      this.$nextTick(() => {
        this.$refs.ancientWharfAddForm.initData(row);
      });
    },
    columnChange() {
      this.$refs.tableList.refreshColumn();
    },
    //复选框事件(保留翻页选中的数据)
    selectChangeEvent({ checked, records, reserves, row }) {
      // console.log(checked ? '勾选事件' : '取消事件')
      // console.log('当前选中的数据：' + records)
      // console.log('翻页时其他页的数据：' + reserves)
      // console.log('当前选中的数据行：' + row )
      //勾选选中时
      if (checked) {
        //第一次选数据，还未进行翻页时
        if (reserves.length == 0) {
          this.selectedRowKeys = records.map((v) => v.aac147);
          this.selectionRows = records;
        } else {
          //aac147集合，翻页存在已选中的数据时,拼接新选中的数据
          this.selectedRowKeys = [
            ...reserves.map((v) => v.aac147),
            ...records.map((v) => v.aac147),
          ];
          //数据集合，翻页存在已选中的数据时,拼接新选中的数据
          this.selectionRows = [...reserves, ...records];
        }
      } else {
        //取消选中时
        let aac147Index = this.selectedRowKeys.indexOf(row.aac147);
        if (aac147Index > -1) {
          //删除取消选中删除指定元素aac147
          this.$delete(this.selectedRowKeys, aac147Index);
        }

        let dataIndex = null;
        for (let i = 0; i < this.selectionRows.length; i++) {
          if (this.selectionRows[i].aac147 == row.aac147) {
            dataIndex = i;
            break;
          }
        }
        //删除取消选中的元素整个对象
        this.$delete(this.selectionRows, dataIndex);
      }
    },
    //vxe复选框当前页全选中方法(保留翻页选中的数据)：
    selectAllEvent({ checked, records, reserves }) {
      // console.log(checked ? '勾选事件' : '取消事件')
      // console.log('当前选中的数据：' + records)
      // console.log('翻页时其他页的数据：' + reserves)
      //全选中时
      if (checked) {
        //第一次选数据，还未进行翻页时
        if (reserves.length == 0) {
          this.selectedRowKeys = records.map((v) => v.aac147);
          this.selectionRows = records;
        } else {
          //aac147集合，翻页存在已选中的数据时,拼接新选中的数据
          this.selectedRowKeys = [
            ...reserves.map((v) => v.aac147),
            ...records.map((v) => v.aac147),
          ];
          //数据集合，翻页存在已选中的数据时,拼接新选中的数据
          this.selectionRows = [...reserves, ...records];
        }
      } else {
        //取消全选时,直接将翻页数据赋值，当前页数据不用加上
        this.selectionRows = reserves;
        this.selectedRowKeys = reserves.map((v) => v.aac147);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.permission {
  //padding: 15px 20px 0;
  height: 100%;

  .desc,
  .operate {
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #fff;

    > div {
      flex: 0 0 auto;
      display: flex;
      align-items: center;
    }
  }

  .desc {
    margin-bottom: 20px;
  }

  .operate {
    margin-bottom: 15px;
    .field {
      flex: 0 0 auto;
      position: relative;
      margin-left: 15px;
      .field-box {
        position: absolute;
        top: 40px;
        left: 0;
        right: -40px;
        z-index: 99;
        border: 1px solid #2175a9;
        background: #002a44;
        border-radius: 5px;
        ul {
          padding: 10px;
          li {
            padding: 1px 0;
          }
        }
        .btn {
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 5px;
          border-top: 1px solid #2175a9;
          color: #fff;
          p {
            flex: 1;
            text-align: center;
            cursor: pointer;
            &:hover {
              color: #49bbeb;
            }
          }
        }
      }
    }
  }
  .ep-table {
    height: calc(100% - 60px);
  }
}
// /*表格 筛选-输入框*/
// ::v-deep .vxe-table--filter-wrapper {
//   input {
//     background: #002a44;
//     padding-left: 6px;
//     line-height: 32px;
//     box-shadow: none !important;
//     border: none !important;
//   }
// }
</style>
